package ldh.im.fxgui.demo;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.data.pojo.status.ImMessageActionStatus;
import ldh.im.fxbase.data.pojo.status.ImMessageStatus;
import ldh.im.fxbase.util.ImCacheFactory;
import ldh.im.fxgui.component.message.MessageListViewHandler;
import ldh.im.fxgui.component.message.MessageVirtualFlowView;
import ldh.im.fxgui.util.UiUtil;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class MessageListViewDemo extends Application {

    private MessageListViewHandler messageListViewHandler;
    private AtomicInteger index = new AtomicInteger(0);
    private AtomicLong id = new AtomicLong(0);
    private ScheduledExecutorService scheduledExecutorService = null;

    @Override
    public void start(Stage primaryStage) throws Exception {
        ImUser loginUser = buildUser(1L, "demo001");
        UiUtil.setLoginImUser(loginUser);

        Pane container = buildUi();
        Scene scene = new Scene(container, 600, 400);
        scene.getStylesheets().add(this.getClass().getResource("/css/message-send.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setOnCloseRequest((e)->{
            Platform.exit();
            System.exit(0);
        });
        primaryStage.show();
    }

    private BorderPane buildUi() {
        BorderPane borderPane = new BorderPane();
        messageListViewHandler = new MessageVirtualFlowView(null, null);
        ImSession session = new ImSession();
        session.setId(1L);
        messageListViewHandler.setImSession(session);

        Node messageListView = messageListViewHandler.createMessagePaneNode();
        messageListView.setMouseTransparent(false);
        borderPane.setCenter(messageListView);

        HBox bottomPane = new HBox(10);
        Button b1 = new Button("add message");
        b1.setOnAction((e)->{
            ImMessage m = buildMessage();
            messageListViewHandler.addMessage(m);
        });
        bottomPane.getChildren().add(b1);

        Button b2 = new Button("add messages");
        b2.setOnAction((e)->{
            try {
                if (scheduledExecutorService == null) {
                    scheduledExecutorService = Executors.newScheduledThreadPool(1);
                }
                scheduledExecutorService.scheduleAtFixedRate(()->{
                    Platform.runLater(()->{
                        ImMessage m = buildMessage();
                        messageListViewHandler.addMessage(m);
                    });
                }, 0, 100, TimeUnit.MILLISECONDS);
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        });
        bottomPane.getChildren().add(b2);

        Button b3 = new Button("stop");
        b3.setOnAction((e)->{
            if (scheduledExecutorService != null) {
                scheduledExecutorService.shutdownNow();
                scheduledExecutorService.shutdownNow();
                scheduledExecutorService = null;
            }
        });
        bottomPane.getChildren().add(b3);

        borderPane.setBottom(bottomPane);
        return borderPane;
    }

    private ImUser buildUser(Long id, String userName) {
        ImUser user = new ImUser();
        user.setId(id);
        user.setUserId(userName);
        user.setUserName(userName);

        ImCacheFactory.getInstance().putUser(userName, user);
        return user;
    }

    private ImMessage buildMessage() {
        ImMessage m = new ImMessage();
        m.setMessageType(MessageType.Text);
        m.setContent("hell world " + index.incrementAndGet());
        m.setActionStatus(ImMessageActionStatus.readed);
        m.setStatus(ImMessageStatus.readed);
        m.setServerReceiveTime(new Date());
        m.setFromImUser(buildUser(1L, "demo001"));
        m.setId(id.incrementAndGet());
        m.setClientId(m.getId().toString());

        ImSession session2 = new ImSession();
        session2.setId(1L);
        m.setImSession(session2);
        return m;
    }
}
